package com.editso.haier.controller.admin;

import com.editso.haier.annon.Admin;
import com.editso.haier.annon.RequireLogin;
import com.editso.haier.bean.TopicBean;
import com.editso.haier.result.Result;
import com.editso.haier.service.TopicService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Map;


@Admin
@RestController
@RequestMapping("/topic")
@RequiresRoles("admin")
public class TopicController {
    private TopicService topicService;

    @Autowired
    public void setTopicService(TopicService topicService) {
        this.topicService = topicService;
    }

    @RequireLogin
    @PostMapping
    public Result post(@RequestParam(defaultValue = "default") String type,
                       @RequestBody TopicBean topicBean){
        Subject subject = SecurityUtils.getSubject();
        if ("default".equals(type)){
            return topicService.insertTopic(topicBean, subject.getPrincipal().toString());
        }else if ("cascade".equals(type))
            return topicService.cascadeInsertTopic(topicBean, subject.getPrincipal().toString());
        return Result.failure(topicBean);
    }

    /**
     * @param id 详细信息
     * @param cascade 级联格式 (默认)
     * @param lazy 懒加载数据
     * */
    @GetMapping(path = {"/{id}", ""})
    public Result queryTopic(@PathVariable(required = false) Integer id,
                              @RequestParam(defaultValue = "false") Boolean lazy,
                              @RequestParam(defaultValue = "true") Boolean cascade,
                              @RequestParam(required = false) Integer page,
                              @RequestParam(required = false) Integer size,
                              @RequestParam(defaultValue = "false")Boolean secondary){
        return id == null
                ? topicService.queryAllTopic(lazy, cascade, page, size, secondary)
                : topicService.detailsTopic(id, lazy, secondary);
    }
}
